package com.ruoyi.course.service.impl;

import java.util.List;

import com.ruoyi.common.constant.SysConstants;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.mapper.SysDictDataMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.course.mapper.SysTopicMapper;
import com.ruoyi.course.domain.SysTopic;
import com.ruoyi.course.service.ISysTopicService;

/**
 * 题目Service业务层处理
 * 
 * @author YJF
 */
@Service
public class SysTopicServiceImpl implements ISysTopicService 
{
    private static final Logger log = LoggerFactory.getLogger(SysTopicServiceImpl.class);
    @Autowired
    private SysTopicMapper sysTopicMapper;

    @Autowired
    private SysDictDataMapper dictDataMapper;

    /**
     * 查询题目
     * 
     * @param topicId 题目主键
     * @return 题目
     */
    @Override
    public SysTopic selectSysTopicByTopicId(Long topicId)
    {
        return sysTopicMapper.selectSysTopicByTopicId(topicId);
    }

    /**
     * 查询题目列表
     * 
     * @param sysTopic 题目
     * @return 题目
     */
    @Override
    public List<SysTopic> selectSysTopicList(SysTopic sysTopic)
    {
        sysTopic.setDelFlag(SysConstants.CONSTANT_STR0);
        return sysTopicMapper.selectSysTopicList(sysTopic);
    }

    /**
     * 新增题目
     * 
     * @param sysTopic 题目
     * @return 结果
     */
    @Override
    public int insertSysTopic(SysTopic sysTopic)
    {
        sysTopic.setCreateTime(DateUtils.getNowDate());
        return sysTopicMapper.insertSysTopic(sysTopic);
    }

    /**
     * 导入数据
     *
     * @param topicList 数据列表
     * @param operName 操作用户
     * @return 结果
     */
    @Override
    public String importTopic(List<SysTopic> topicList, String operName)
    {
        if (StringUtils.isNull(topicList) || topicList.size() == 0)
        {
            throw new ServiceException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (SysTopic topic : topicList)
        {
            try
            {
                //根据题目类型名称查键值
                SysDictData dictData = new SysDictData();
                dictData.setDictType("sys_topic_type");
                dictData.setDictLabel(topic.getTopicTypeName());
                dictData.setStatus(SysConstants.CONSTANT_STR0);
                List<SysDictData> dictDataList = dictDataMapper.selectDictDataList(dictData);
                if (StringUtils.isNull(dictDataList)) {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、题目名为 " + topic.getTopicName() + "的题目类型为"+ topic.getTopicTypeName() +"的类型不存在");
                }

                SysTopic sysTopic = new SysTopic();
                sysTopic.setTopicName(topic.getTopicName());
                sysTopic.setTopicIntro(topic.getTopicIntro());
                sysTopic.setTopicSort(topic.getTopicSort());
//                sysTopic.setTopicType(topic.getTopicType());
                sysTopic.setTopicType(Integer.parseInt(dictDataList.get(0).getDictValue()));
                sysTopic.setTopicHard(topic.getTopicHard());
                sysTopic.setTopicAnswer(topic.getTopicAnswer());
                sysTopic.setTopicSource(SysConstants.CONSTANT_INT1);    //导入
                sysTopic.setTopicStatus(topic.getTopicStatus());
                sysTopic.setTopicRemark(topic.getTopicRemark());
                sysTopic.setCreateTime(DateUtils.getNowDate());
                sysTopicMapper.insertSysTopic(sysTopic);
                sysTopic.setUpdateTime(DateUtils.getNowDate());
                successNum++;
                successMsg.append("<br/>" + successNum + "、题目 " + topic.getTopicName() + " 导入成功");
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、题目 " + topic.getTopicName() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    /**
     * 修改题目
     * 
     * @param sysTopic 题目
     * @return 结果
     */
    @Override
    public int updateSysTopic(SysTopic sysTopic)
    {
        sysTopic.setUpdateTime(DateUtils.getNowDate());
        return sysTopicMapper.updateSysTopic(sysTopic);
    }

    /**
     * 批量删除题目
     * 
     * @param topicIds 需要删除的题目主键
     * @return 结果
     */
    @Override
    public int deleteSysTopicByTopicIds(Long[] topicIds)
    {
        return sysTopicMapper.deleteSysTopicByTopicIds(topicIds);
    }

    /**
     * 删除题目信息
     * 
     * @param topicId 题目主键
     * @return 结果
     */
    @Override
    public int deleteSysTopicByTopicId(Long topicId)
    {
        return sysTopicMapper.deleteSysTopicByTopicId(topicId);
    }
}
